package com.cnfilm.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cnfilm.web.film.Film;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedOutput;


/**
 * 文件名：RssUtils.java 网站RSS生成
 * 版本信息：V1.0
 * 日期：2013-06-18
 * Copyright BDVCD Corporation 2013
 * 版权所有 http://www.bdvcd.com
 */
public class RssUtils {
	
	public static String getRssString(List<Film> filmList,HashMap<String,String> map){
		
        Channel channel = new Channel("rss_2.0");
        channel.setTitle(map.get("title"));
        channel.setDescription(map.get("description"));
        channel.setLink("http://www.bdvcd.com/");
        channel.setEncoding("UTF-8");
        /**这个list对应rss中的item列表**/
        List<Item> items = new ArrayList<Item>();
        /**新建Item对象，对应rss中的<item></item>**/
        Item item = null;
        for(Film film:filmList){
        	item = new Item();
        	item.setAuthor(film.getStr("starring"));
        	item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html");
        	item.setPubDate(DateUtils.parse(film.getStr("addtime")));
            item.setTitle(film.getStr("fname"));
            Description description = new Description();
            description.setValue(film.getStr("content"));
            item.setDescription(description);
            items.add(item);
        }
        channel.setItems(items);
        /**用WireFeedOutput对象输出rss文本**/
        WireFeedOutput out = new WireFeedOutput();
        String rssString = "";
        try {
        	rssString = out.outputString(channel);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (FeedException e) {
            e.printStackTrace();
        }
        return rssString;
	}
}